using UnityEngine;
using System;

public class MapUIHandler : GameUIHandler< MapUIHandler >
{
	void Awake ()
	{
		mInstance = this;

		Show();
	}

	Texture2D texture2D = null;
	Texture2D miniTexture2D = null;

	int w = 164;
	int h = 158;

	UITexture uiTexture = null;

	public void setMapID( int id )
	{
		miniTexture2D = new Texture2D( w , h , TextureFormat.RGB24 , true );
		texture2D = (Texture2D)Resources.Load( GameSetting.MiniMapPath + id );

		if ( uiTexture ) 
		{
			uiTexture.mainTexture = miniTexture2D;
		}
	}

	public void setMapXY( float x , float y )
	{
		if ( !isShow || texture2D == null ) 
		{
			return;
		}

		if ( uiTexture == null ) 
		{
			uiTexture = uiObject.GetComponentInChildren< UITexture >();
			uiTexture.mainTexture = miniTexture2D;
		}

		//Debug.Log( x + " " + y );

		x -= (float)w / (float)texture2D.width * 0.495f;
		y -= (float)h / (float)texture2D.height * 0.50f;

		int xx = (int)( x * texture2D.width );
		int yy = (int)( y * texture2D.height );

		if ( xx + w >= texture2D.width ) 
		{
			xx = texture2D.width - w - 1;
		}

		if ( yy + h >= texture2D.height ) 
		{
			yy = texture2D.height - h - 1;
		}

		if ( xx < 0 ) 
		{
			xx = 0;
		}

		if ( yy < 0 ) 
		{
			yy = 0;
		}

		//Debug.Log( xx + " " + yy );

	  	Color[] color = texture2D.GetPixels( xx , yy , w , h );
		miniTexture2D.SetPixels( color );
		miniTexture2D.Apply();
	}

	public override void onRelease()
	{
		
	}
	public override void onInit()
	{

	}
	public override void onOpen()
	{
		
	}
	public override void onClose()
	{
		
	}



}
